package com.demboos.galaxy.radio.widget;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;



public class WidgetConfigActivity extends Activity {
	
	RadioButton mRadioEurope;
	RadioButton mRadioUSA;
	RadioButton mRadioJapan;
	
	GalaxyRadioService mService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mService = GalaxyRadioService.INSTANCE;
		
		if (mService == null) {
			Toast t = Toast.makeText(this, "At least one radio widget must be present on home screen in order to configure the location setting", 0);
			t.show();
			
			finish();
			
			return;
		}

		setContentView(R.layout.config_activity);
		
		mRadioEurope = (RadioButton) findViewById(R.id.RadioButtonLocationEurope);
		mRadioUSA = (RadioButton) findViewById(R.id.RadioButtonLocationUSA);
		mRadioJapan = (RadioButton) findViewById(R.id.RadioButtonLocationJapan);
		
		if (WidgetConfigDatabase.hasRadioRegionPref(mService)) {
			if (WidgetConfigDatabase.REGION_EUROPE.equals(WidgetConfigDatabase.getRadioRegionPref(mService))) {
				mRadioEurope.setChecked(true);
			} else if (WidgetConfigDatabase.REGION_USA.equals(WidgetConfigDatabase.getRadioRegionPref(mService))) {
				mRadioUSA.setChecked(true);
			} else if (WidgetConfigDatabase.REGION_JAPAN.equals(WidgetConfigDatabase.getRadioRegionPref(mService))) { 
				mRadioJapan.setChecked(true);
			}
		} else {
			mRadioEurope.setChecked(true);
		}
		
		findViewById(R.id.ButtonSave).setOnClickListener(mSaveOnClickListener);
		findViewById(R.id.ButtonCancel).setOnClickListener(mCancelOnClickListener);
	}
	
	View.OnClickListener mSaveOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			if (mRadioEurope.isChecked()) {
				WidgetConfigDatabase.setRadioRegionPref(mService, WidgetConfigDatabase.REGION_EUROPE);
			} else if (mRadioUSA.isChecked()) {
				WidgetConfigDatabase.setRadioRegionPref(mService, WidgetConfigDatabase.REGION_USA);
			} else if (mRadioJapan.isChecked()) {
				WidgetConfigDatabase.setRadioRegionPref(mService, WidgetConfigDatabase.REGION_JAPAN);
			}
			
			GalaxyRadioAppWidgetProvider.getInstance().performUpdate(mService, null);
			
			finish();
		}
	};
	
	View.OnClickListener mCancelOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};
	
}
